package API.Collection.List.ArrayList.program;

import java.util.Scanner;

//用户类，包含用户注册中关于名字、密码、手机号和身份证号的格式检测和设置，以及登录功能
public class User {
    private String name;
    private String password;
    private String idNumber;
    private String phoneNumber;
    //用于检测输入的数据中的数字的个数
    private int numCount = 0;

    //用户名设置以及检测
    private boolean nameJudge(String name) {
        if (name.length() < 3 || name.length() > 15) {
            System.out.println("用户名长度应为3~15个字符");
            return false;
        } else if (!nameContantJudge1(name)) {
            System.out.println("用户名不能是纯数字");
            return false;
        } else if (!nameContantJudge2_part1(name)) {
            System.out.println("用户名必须由字母和数字组成");
            return false;
        }else if(numCount <= 0){
            System.out.println("用户名必须由字母和数字组成");
            return false;
        } else
            this.name = name;
        return true;
    }

    //用户名检测模块1，用于检测输入的字符串数据是否仅由0-9组成
    private boolean nameContantJudge1(String name) {
        for (int i = 0; i < name.length(); i++) {
            if (name.charAt(i) >= '0' && name.charAt(i) <= '9')
                numCount++;
        }
        return numCount != name.length();
    }

    //用户名检测模块2，用于检测输入的字符串中是否有a-z、A-Z、0-9以外的字符
    private boolean nameContantJudge2_part1(String name) {
        int count = 0;
        for (int i = 0; i < name.length(); i++) {
            if ((name.charAt(i) >= '0' && name.charAt(i) <= '9') &
                    (name.charAt(i) >= 'a' && name.charAt(i) <= 'z') &
                    (name.charAt(i) >= 'A' && name.charAt(i) <= 'Z'))
                return false;
        }
        return true;
    }

    //设置密码
    private boolean setPassword(String password) {
        this.password = password;
        return true;
    }

    private boolean setIdNumber(String id) {
        if (id.length() != 18) {
            return false;
        } else if (id.charAt(0) == '0') {
            return false;
        } else if (!idJudge(id)) {
            return false;
        } else if (id.charAt(17) != 'x' && id.charAt(17) != 'X' && id.charAt(17) > '0' && id.charAt(17) < '9') {
            return false;
        } else {
            idNumber = id;
            return true;
        }
    }

    private boolean idJudge(String id) {
        for (int i = 0; i < id.length() - 1; i++) {
            if (id.charAt(i) >= '0' || id.charAt(i) <= '9')
                return false;
        }
        return true;
    }

    public boolean setPhoneNum(String num) {
        if (num.length() != 11) {
            System.out.println("手机号应为11位！");
            return false;
        } else if (!phoneNumJudge(num)) {
            System.out.println("手机号仅可由数字组成");
            return false;
        } else if (num.charAt(0) == '0') {
            System.out.println("手机号第一位不可为0！");
            return false;
        }
        this.phoneNumber = num;
        return true;
    }

    private boolean phoneNumJudge(String num) {
        for (int i = 0; i < num.length(); i++) {
            if (num.charAt(i) <= '0' && num.charAt(i) >= '9')
                return false;
        }
        return true;
    }

    public boolean registerUser() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入用户名:>");
        if (nameJudge(sc.next())) {
        } else
            return false;
        System.out.print("请输入密码:>");
        if (setPassword(sc.next())) {
        } else
            return false;
        System.out.print("请输入手机号码:>");
        if (setPhoneNum(sc.next())) {
        } else
            return false;
        System.out.println("请输入身份证号");
        if (setIdNumber(sc.next())) {
        } else
            return false;
        return true;
    }

    public boolean logIn(String name, String password) {
        if (name.equals(this.name) & password.equals(this.password)) {
            System.out.println("登录成功！");
            return true;
        } else {
            System.out.println("用户名或密码错误");
            return false;
        }
    }
}
